package com.anhry.app.safety.util.service.impl;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.anhry.app.safety.manage.system.bean.TbActionLog;
import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.bean.TbUserDept;
import com.anhry.app.safety.util.DateFormater;
import com.anhry.app.safety.util.Param;
import com.anhry.app.safety.util.dao.UtilDao;
import com.anhry.app.safety.util.service.UtilService;

@Service
public class UtilServiceImpl implements UtilService{
	
	private Logger logger = Logger.getLogger(UtilServiceImpl.class);
	@Resource
	private UtilDao utilDao;

	public UtilDao getUtilDao() {
		return utilDao;
	}

	public void setUtilDao(UtilDao utilDao) {
		this.utilDao = utilDao;
	}

	public void delete(Object obj) throws Exception {
		this.utilDao.delete(obj);
		
	}

	public Object getObjectById(Class classes, String id) throws Exception {
		return this.utilDao.getObjectById(classes, id);
	}

	public Object getObjectById(Class classes, int id) throws Exception {
		return this.utilDao.getObjectById(classes, id);
	}

	public void save(Object obj) throws Exception {
		this.utilDao.save(obj);
		
	}

	public void update(Object obj) throws Exception {
		this.utilDao.update(obj);
		
	}

	public Object getObjectById(Class classes, Long id) throws Exception {
		return this.utilDao.getObjectById(classes, id);
	}

	public Object getHqlQuery(String hql) throws Exception {
		return this.utilDao.getHqlQuery(hql);
	}

	public Collection getHqlQuery(String hql, int start, int count) throws Exception {
		return this.utilDao.getHqlQuery(hql, start, count);
	}

	public Object getCollection(String objName, List<Param> params, Map orders, int start, int count) throws Exception {
		return this.utilDao.getCollection(objName, params, orders, start, count);
	}

	public int getHqlQuerySize(String hql) throws Exception {
		return this.utilDao.getHqlQuerySize(hql);
	}

	public void deleteAll(String objName, List<Param> params) throws Exception {
		this.utilDao.deleteAll(objName, params);
	}

	public void updateAll(String objName, Map sets, List<Param> params) throws Exception {
		this.utilDao.updateAll(objName, sets, params);
	}

	public void saveOrUpdate(Object obj) throws Exception {
		this.utilDao.saveOrUpdate(obj);
	}

	public List getFjList(String classes, String id) throws Exception {
		return this.utilDao.getFjList(classes, id);
	}

	public void deleteBySql(String sql) throws Exception {
		this.utilDao.deleteBySql(sql);
	}

	public List getObjectBySql(String sql, int start, int count)throws Exception {
		if(logger.isDebugEnabled()) {
			logger.debug("Query sql is ---> " + sql);
		}
		return this.utilDao.getObjectBySql(sql,start,count);
	}
    public <T> T getObject(Class<T> clazz, String ID) throws Exception {
	    return utilDao.getObject(clazz,ID);
    }
    
	public String getObjectBySql(String sql) throws Exception {
		return this.utilDao.getObjectBySql(sql);
	}

	public List getObjecList(String table, String hql, String order, int start,int count) throws Exception {
		StringBuffer hqls = new StringBuffer();
		if(count == 0){
			hqls.append("select count(*) from "+table+" t ").append(hql);
		}else{
			hqls.append("from "+table+" t ").append(hql).append(order);
		}
		return (List)this.utilDao.getHqlQuery(hqls.toString(), start, count);
	}

	public List getCorpTypeList(TbUser user, List typeList) throws Exception {
		try {
			String grade = user.getTbUserGrade().getId().toString();
			TbUserDept dept = null;
			TbUserDept unit = null;
			if(null!=user.getTbUserUnit()){
				unit = user.getTbUserUnit();
			}
			if(null!=user.getTbUserDept()){
				dept = user.getTbUserDept();
			}
			if("3".equals(grade) && "3".equals(dept.getDeptClass())){//市科室并且是行业监管的科室
				
			}else if("4".equals(grade)){//区科室
				
			}else if("5".equals(grade)){//市行业
				
			}else if("6".equals(grade)){//区行业
				
			}else{
				return typeList;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return typeList;
	}

	public List getQuDeptList(TbUser user, List deptList) throws Exception {
		return null;
	}
	
	public void evict(Object o) throws Exception {
		this.getUtilDao().evict(o);
	}
	
	public void merge(Object o) throws Exception {
		this.getUtilDao().merge(o);
	}

	public void saveActionLog(TbUser user, String log) throws Exception {
		TbActionLog obj = new TbActionLog();
		obj.setActionDate(new Date());
		obj.setActionInfo(DateFormater.formatDateTime(new Date())+log);
		obj.setIsDele("0");
		obj.setUserId(user.getId());
		obj.setUserName(user.getUserName());
		this.utilDao.save(obj);
	}
	

}
